package com.excesys.exsecs.access.controller;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.excesys.exsecs.access.dto.AccessFloorDTO;
import com.excesys.exsecs.access.entity.AccessFloor;
import com.excesys.exsecs.access.util.BeanMapUtil;
import com.excesys.exsecs.access.vo.AccessFloorExcelVO;
import com.excesys.exsecs.common.core.util.R;
import com.excesys.exsecs.common.excel.annotation.RequestExcel;
import com.excesys.exsecs.common.log.annotation.SysLog;
import com.excesys.exsecs.access.service.AccessFloorService;
import org.springframework.security.access.prepost.PreAuthorize;
import com.excesys.exsecs.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 区域管理-楼层管理
 *
 * @author lxc
 * @date 2024-09-13 13:56:54
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/accessFloor" )
@Tag(description = "accessFloor" , name = "区域管理-楼层管理管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class AccessFloorController {

    private final  AccessFloorService accessFloorService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param bamSFloor 区域管理-楼层管理
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('access_accessFloor_view')")
    public R getFloorPage(Page page, AccessFloorDTO bamSFloor) {
        return R.ok(accessFloorService.getFloorVosPage(page, bamSFloor));
    }

    /**
     * 通过id查询区域管理-楼层管理
     * @param floorId id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{floorId}")
    @PreAuthorize("@pms.hasPermission('access_accessFloor_view')")
    public R getById(@PathVariable("floorId") String floorId) {
        return R.ok(accessFloorService.getById(floorId));
    }

    /**
     * 新增区域管理-楼层管理
     * @param bamSFloor 区域管理-楼层管理
     * @return R
     */
    @Operation(summary = "新增区域管理-楼层管理", description = "新增区域管理-楼层管理")
    @SysLog("新增区域管理-楼层管理")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('access_accessFloor_add')")
    public R save(@RequestBody AccessFloor bamSFloor) {
        return R.ok(accessFloorService.save(bamSFloor));
    }

    @Operation(summary = "根据条件查询不分页", description = "条件查询")
    @PostMapping("/getBamFloorBy")
    @PreAuthorize("@pms.hasPermission('access_accessFloor_view')")
    public R getFloorBy(@RequestBody AccessFloorDTO bamSFloor) {
        return R.ok(accessFloorService.getFloorVos(bamSFloor));
    }

    /**
     * 修改区域管理-楼层管理
     * @param bamSFloor 区域管理-楼层管理
     * @return R
     */
    @Operation(summary = "修改区域管理-楼层管理", description = "修改区域管理-楼层管理")
    @SysLog("修改区域管理-楼层管理")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('access_accessFloor_edit')")
    public R updateById(@RequestBody AccessFloor bamSFloor) {
        return R.ok(accessFloorService.updateById(bamSFloor));
    }

    /**
     * 通过id删除区域管理-楼层管理
     * @param floorId id
     * @return R
     */
    @Operation(summary = "通过id删除区域管理-楼层管理", description = "通过id删除区域管理-楼层管理")
    @SysLog("通过id删除区域管理-楼层管理")
    @DeleteMapping("/{floorId}")
    @PreAuthorize("@pms.hasPermission('access_accessFloor_del')")
    public R removeById(@PathVariable String floorId) {
        return R.ok(accessFloorService.removeById(floorId));
    }

    @Operation(summary = "通过ids删除区域管理-楼层管理", description = "通过id删除区域管理-楼层管理")
    @SysLog("通过ids删除区域管理-楼层管理")
    @DeleteMapping("/deleteIds")
    @PreAuthorize("@pms.hasPermission('access_accessFloor_del')")
    public R removeByIds(@RequestBody List<String> ids) {
        return R.ok(accessFloorService.removeByIds(ids));
    }

    /**
     * 导出excel 表格
     * @return
     */
    @Operation(summary = "导出所有数据", description = "导出所有数据")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('access_accessFloor_export')")
    public List<AccessFloorExcelVO> export(AccessFloorDTO bamSFloor) {
        return accessFloorService.listFloor(bamSFloor);
    }

    /**
     * 导入楼层
     * @param excelVOList 楼层管理列表
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('access_accessFloor_import')")
    public R importRole(@RequestExcel List<AccessFloorExcelVO> excelVOList, BindingResult bindingResult) {
        return accessFloorService.importFloor(excelVOList, bindingResult);
    }

    /**
     * 不分页查询楼层 liuzhikun
     * @return
     */
    @Operation(summary = "不分页查询楼层", description = "不分页查询楼层")
    @GetMapping("/listByWhere")
    public R<List<Map<String, Object>>> getBamSFloorList(@RequestParam Map param) throws Exception {
        AccessFloor bamSFloor = new AccessFloor();
        if (ObjectUtil.isNotNull(param) && !param.isEmpty())
            bamSFloor = BeanMapUtil.mapToBean(param, AccessFloor.class);
        List<AccessFloor> dataList = accessFloorService.list(Wrappers.query(bamSFloor).orderByAsc("floor_code"));
        List<Map<String, Object>> mapList = BeanMapUtil.objectListToListMap(dataList);
        return R.ok(mapList, "不分页查询楼层");
    }

    @Operation(summary = "自动生成楼层编码", description = "自动生成楼层编码")
    @GetMapping("/getFloorCode")
    @PreAuthorize("@pms.hasPermission('access_accessFloor_view')")
    public R getBuildingCode(String code, int level, String suffix, int binary) throws ParseException {
        return R.ok(accessFloorService.GetFloorCodeBy(code, level, suffix, binary));
    }
}